package sto.orz.rptp.model;

import javax.persistence.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.lang.builder.ToStringBuilder;

import sto.orz.html.Element;

@Entity
@Table(name = "rpt_definition")
public class Definition implements Serializable, Comparable<Definition> {
	private static final long serialVersionUID = 241359727615142005L;
	
	@Element()
	private List<Attribute> attributes = new ArrayList<Attribute>();
	
	@Element()
	private Set<Dataset> datasets = new HashSet<Dataset>();
	
	@Element()
	private String id;
	
	@Element()
	private String name = "新建报表方案";

	public Definition() {
	}

	public Definition(String name) {
		this.name = name;
	}

	public int compareTo(Definition o) {
		if (o.getId() == this.getId())
			return 0;
		return o.getName().compareTo(this.getName());
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Definition other = (Definition) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinTable(name = "rptd_rpta", joinColumns = { @JoinColumn(name = "rptd_id") }, inverseJoinColumns = { @JoinColumn(name = "rpta_id") })
	public List<Attribute> getAttributes() {
		return attributes;
	}

	@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
	@JoinTable(name = "rpt_definition_dataset", joinColumns = { @JoinColumn(name = "rptd_id") }, inverseJoinColumns = { @JoinColumn(name = "dataset_id") })
	public Set<Dataset> getDatasets() {
		return datasets;
	}

	@Id
	@GeneratedValue(generator = "system-uuid")
	@org.hibernate.annotations.GenericGenerator(name = "system-uuid", strategy = "uuid")
	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	public void setAttributes(List<Attribute> attribute) {
		this.attributes = attribute;
	}

	public void setDatasets(Set<Dataset> datasets) {
		this.datasets = datasets;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
				.appendSuper(super.toString()).append("id", id).append("name",
						name).append("attributes", attributes).append(
						"datasets", datasets).toString();
	}
}
